package servlet;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fulbitofacil.exception.DAOExcepcion;
import fulbitofacil.modelo.CanchaFul;
import fulbitofacil.modelo.Cliente;
import fulbitofacil.modelo.HorarioAtencion;
import fulbitofacil.modelo.Persona;
import fulbitofacil.negocio.AlquilerNegocio;

/**
 * Servlet implementation class AlquilerServlet
 */
@WebServlet("/AlquilerServlet")
public class AlquilerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AlquilerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		java.util.Date hoy = new java.util.Date();
		Date fechaAlquiler = new Date(hoy.getTime());
		Date fecha = Date.valueOf(request.getParameter("qryFecha"));
		//int hora = Integer.valueOf(request.getParameter("qryHora"));
		int idCanchaFul = Integer.valueOf(request.getParameter("qryidCancha"));
		int idHorario = Integer.valueOf(request.getParameter("qryidHora"));
		CanchaFul objCanchaFul = new CanchaFul();
		objCanchaFul.setIdCanchaFul(idCanchaFul);
		HorarioAtencion objHorario = new HorarioAtencion();
		objHorario.setIdHoraAtencion(idHorario);
		
		

		HttpSession sesion = request.getSession();
		Cliente idCliente = new Cliente();
		Persona perso = (Persona)sesion.getAttribute("Persona");
		
		idCliente.setId_persona(perso.getId_persona());
		String ruta = "Alquiler.jsp";
		AlquilerNegocio negocio = new AlquilerNegocio();
		try {
			String mensaje = "";
			mensaje = negocio.insertarAlquiler(fechaAlquiler, fecha, objCanchaFul, idCliente, objHorario);			
			RequestDispatcher rd = request.getRequestDispatcher(ruta);
			rd.forward(request, response);
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher(ruta);
			rd.forward(request, response);
		}

		
		
	}

}
